package com.party.mobile.web.controller.photo;

import com.party.api.dto.wechat.requst.XcxQrCodeRequest;
import com.party.common.constant.Constant;
import com.party.common.utils.PartyCode;
import com.party.common.utils.StringUtils;
import com.party.core.model.photo.PhotoProject;
import com.party.core.model.photo.PhotosImgInfo;
import com.party.core.model.wechatPlatform.WechatOpenMember;
import com.party.core.service.photo.IPhotoProjectService;
import com.party.core.service.photo.IPhotosImgInfoService;
import com.party.core.service.wechatPlatform.IWechatOpenMemberService;
import com.party.mobile.biz.file.FileBizService;
import com.party.mobile.web.dto.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description: 分享
 * @Author: yangshoukun
 * @Date: 2018/6/5 9:57
 */
@Controller
@RequestMapping(value = "/photos/share")
public class ShareController {
    @Autowired
    private IPhotosImgInfoService photosImgInfoService;
    @Autowired
    private IWechatOpenMemberService wechatOpenMemberService;
    @Autowired
    private IPhotoProjectService photoProjectService;
    @Autowired
    private FileBizService fileBizService;

    Logger logger = LoggerFactory.getLogger(getClass());

    @ResponseBody
    @RequestMapping("qrcode")
    public AjaxResult qrcode(String imgId) {
        if (StringUtils.isEmpty(imgId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "参数不能为空");
        }

        try {
            PhotosImgInfo imgInfo = photosImgInfoService.get(imgId);
            PhotoProject photoProject = photoProjectService.get(imgInfo.getPhotosId());
            WechatOpenMember openMember = wechatOpenMemberService.findByType(Constant.WECHAT_ACCOUNT_TYPE_SYSTEM_XCX_PHOTO);
            XcxQrCodeRequest qrCodeRequest = new XcxQrCodeRequest();
            qrCodeRequest.setPage("pages/photo_info/index");
            qrCodeRequest.setScene(imgId);
            String filePath = fileBizService.getUploadCIPath(photoProject.getMemberId(), "photo/image");
            String qrcodeUrl = fileBizService.uploadCloudOut(imgId, filePath, qrCodeRequest, openMember.getAuthorizerAccessToken(), 1);
            if (StringUtils.isEmpty(qrcodeUrl)) {
                throw new Exception("生成小程序码异常");
            }
            return AjaxResult.success((Object) qrcodeUrl);
        } catch (Exception e) {
            logger.error("生成小程序码异常", e);
            return AjaxResult.error(100, "生成小程序码异常");
        }
    }

    /**
     * 获取相册小程序码
     *
     * @param photoId 相册id
     * @return
     */
    @ResponseBody
    @RequestMapping("photoQrcode")
    public AjaxResult photoQrcode(String photoId) {
        if (StringUtils.isEmpty(photoId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "参数不能为空");
        }

        try {
            WechatOpenMember openMember = wechatOpenMemberService.findByType(Constant.WECHAT_ACCOUNT_TYPE_SYSTEM_XCX_PHOTO);
            PhotoProject photoProject = photoProjectService.get(photoId);
            String filePath = fileBizService.getUploadCIPath(photoProject.getMemberId(), "photo");
            XcxQrCodeRequest qrCodeRequest = new XcxQrCodeRequest();
            qrCodeRequest.setPage("pages/photo/index");
            qrCodeRequest.setScene(photoProject.getId());
            String result = fileBizService.uploadCloudOut(photoProject.getId(), filePath, qrCodeRequest, openMember.getAuthorizerAccessToken(), 1);
            if (StringUtils.isEmpty(result)) {
                throw new Exception("生成小程序码异常");
            }
            return AjaxResult.success((Object) result);
        } catch (Exception e) {
            logger.error("生成小程序码异常", e);
            return AjaxResult.error(100, "生成小程序码异常");
        }
    }
}
